package com.sbs.gaoxinqutoilet.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.google.gson.reflect.TypeToken;
import com.sbs.gaoxinqutoilet.R;
import com.sbs.gaoxinqutoilet.adapter.StellarMapAdapter;
import com.sbs.gaoxinqutoilet.http.HttpApis;
import com.sbs.gaoxinqutoilet.http.Request;
import com.sbs.gaoxinqutoilet.interfaces.Constants;
import com.sbs.gaoxinqutoilet.interfaces.StellarListener;
import com.sbs.gaoxinqutoilet.utils.HttpUtils;
import com.sbs.gaoxinqutoilet.utils.JsonUtil;
import com.sbs.gaoxinqutoilet.utils.LogUtils;
import com.sbs.gaoxinqutoilet.utils.SPUtil;
import com.sbs.gaoxinqutoilet.view.stellarmap.StellarMap;
import com.zhy.autolayout.AutoRelativeLayout;
import com.zhy.http.okhttp.OkHttpUtils;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.sbs.gaoxinqutoilet.utils.loading.LoadingDialog.cancelLoadingDialog;
import static com.sbs.gaoxinqutoilet.utils.loading.LoadingDialog.showLoadingDialog;

/**
 * Created by 聪明一只哈 on 2018/6/13.
 * Time: 2018/6/13  11:09
 */

public class CheckerActivity extends AppCompatActivity implements StellarListener{
    @BindView(R.id.toolbar_back)
    ImageView toolbarBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar_iv_menu)
    ImageView toolbarIvMenu;
    @BindView(R.id.toolbar_tv_menu)
    TextView toolbarTvMenu;
    @BindView(R.id.toolbar_rl_menu)
    AutoRelativeLayout toolbarRlMenu;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.stellarmap_check_ac)
    StellarMap stellarmapCheckAc;
    private Unbinder unbinder;
    private BuysRequest buyRequest;
    private StellarMapAdapter stellarMapAdapter;
    private String token_data;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_checker);
        unbinder = ButterKnife.bind(this);

        initView();

        initData();

        initListener();

    }




    private void initView() {
        toolbarBack.setVisibility(View.VISIBLE);
        toolbarTitle.setText("选择验收人");

        token_data = SPUtil.getString(Constants.KEY_TOKEN, "");

        stellarMapAdapter = new StellarMapAdapter();

        stellarmapCheckAc.setInnerPadding(6, 6, 6, 6);	// 设置内边距
    }

   private void initData() {
//        new Handler() {
//        }.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                requestRefresh();
//            }
//        }, 500);

       requestRefresh();




    }

    private void initListener() {
//        stellarMapAdapter.setStellarListener(new StellarListener() {
//            @Override
//            public void show(String str) {
//                Intent intent = new Intent();
//                Bundle bundle = new Bundle();
//                bundle.putString("a","aa");
//
//                intent.putExtra("b",bundle);
//                setResult(RESULT_OK,intent);
//                finish();
//            }
//        });

        stellarMapAdapter.setStellarListener(this);
    }

    @Override
    public void show(String str) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("bundle",str);

        intent.putExtra("inters",bundle);
        setResult(RESULT_OK,intent);
        finish();
    }


    @OnClick(R.id.toolbar_back)
    public void onViewClicked() {
        finish();
    }


    /**
     * 每次进页面,刷新数据
     */
    private void requestRefresh() {
       showLoadingDialog(this);
        String mBuyUrl = HttpApis.acceptDetial_accepter + 1 + "&rows=" + 10;
        LogUtils.i("mbuyUrl", mBuyUrl);
        if (buyRequest == null) {
            buyRequest = new BuysRequest();
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("token",token_data);
        HttpUtils.requestGet(this, mBuyUrl,map, buyRequest);

    }



    //请求列表数据
    private class BuysRequest extends Request {
        @Override
        public void success(String jsonStr) {
            super.success(jsonStr);
            //这里是假数据
            //jsonStr= MyFakeData.stellarmap;
            LogUtils.i("stellarmap", jsonStr);
            Type type = new TypeToken<HashMap<Integer, ArrayList<String>>>() {
            }.getType();

            HashMap<Integer, ArrayList<String>> datas = JsonUtil.json2Collection(jsonStr, type);


            if (datas!=null) {
                int max = 0;
                for (Integer key : datas.keySet()) {
                    int size = datas.get(key).size();
                    if (size > max) {
                        max = size;
                    }
                }

                //stellarMapAdapter = new StellarMapAdapter(datas);
                stellarMapAdapter.addData(datas);
                stellarmapCheckAc.setAdapter(stellarMapAdapter);
                stellarmapCheckAc.setRegularity(max, max);	// 设置x和y方向的密度
            }

            cancelLoadingDialog();


        }

        @Override
        public void error(String error) {
            super.error(error);
            cancelLoadingDialog();

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        unbinder.unbind();
        OkHttpUtils.getInstance().cancelTag(this);
    }



}
